#include "MQTTSubscriber.h"
#include <chrono>
#include <string>
#include <thread>

void on_message(struct mosquitto *, void *,
                const struct mosquitto_message *msg) {
  std::string payload((char *)msg->payload, msg->payloadlen);
  std::cout << "📩 收到消息：" << payload << "（主题：" << msg->topic << "）"
            << std::endl;
}

int main() {
  MQTTSubscriber sub("192.168.5.11", "vm_subscriber", "test/topic", 1);
  sub.set_message_callback(on_message);

  if (!sub.connect())
    return 1;
  sub.loop_start();
  sub.subscribe();

  std::cout << "🕓 等待接收消息中（Ctrl+C 退出）..." << std::endl;
  std::this_thread::sleep_for(std::chrono::seconds(60));

  sub.loop_stop();
  sub.disconnect();
  return 0;
}
